package org.nt4b.schemadoc.core;

import javax.sql.DataSource;

import org.nt4b.schemadoc.core.dao.SchemaDocDAO;
import org.nt4b.schemadoc.core.dao.oracle.OracleSchemaDocDAO;
import org.springframework.jdbc.datasource.DriverManagerDataSource;


public class DAOFactory {
    
    /**
     * Builds a new DataSource and CommentsExtractorDAO for Oracle.
     * 
     */
    public static SchemaDocDAO getDAO(final String url, final String username, final String password) {
        final DriverManagerDataSource ds = new DriverManagerDataSource();
        ds.setUrl(url);
        ds.setUsername(username);
        ds.setPassword(password);
        
        return DAOFactory.getDAO(ds, url, username, password);
    }
    
    public static SchemaDocDAO getDAO(final DataSource ds, final String url, final String username, final String password) {
        if (DAOFactory.isOracle(url)) {
            if (ds instanceof DriverManagerDataSource) {
                final DriverManagerDataSource dataSource = (DriverManagerDataSource) ds;
                dataSource.setDriverClassName("oracle.jdbc.OracleDriver");
                dataSource.setUrl(url);
                dataSource.setUsername(username);
                dataSource.setPassword(password);
                
                final OracleSchemaDocDAO oracleDao = new OracleSchemaDocDAO();
                oracleDao.setDataSource(ds);
                return oracleDao;
                
            } else {
                throw new IllegalArgumentException("Unsupported dataSorice " + ds.getClass());
            }
        }
        
        return null;
    }
    
    public static boolean isOracle(final String url) {
        if (url.contains("oracle")) {
            return true;
        }
        return false;
    }
    
}
